package com.woniu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.bank.dto.FarmerLoanSheetDTO;
import com.woniu.bank.entity.AuditForm;
import com.woniu.bank.entity.ClientInfoSheet;
import com.woniu.bank.entity.FarmerLoanSheet;
import com.woniu.bank.mapper.AuditFormMapper;
import com.woniu.bank.mapper.ClientInfoSheetMapper;
import com.woniu.bank.mapper.FarmerLoanSheetMapper;
import com.woniu.bank.service.FarmerLoanSheetService;
import com.woniu.bank.vo.FarmerLoanSheetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author ：lgy
 * @description：TODO
 * @date ：2023/1/4 11:54
 */
@Service
public class FarmerLoanSheetServiceImpl implements FarmerLoanSheetService {
    @Autowired
    private FarmerLoanSheetMapper farmerLoanSheetMapper;
    @Autowired
    private AuditFormMapper auditFormMapper;
    @Autowired
    private ClientInfoSheetMapper clientInfoSheetMapper;

    /**
     * lgy
     * 模糊查询贷款信息
     * @param farmerLoanSheetDTO
     * @return
     */
    @Override
    public List<FarmerLoanSheetVO> selectFlsVOByFlsDTO(FarmerLoanSheetDTO farmerLoanSheetDTO) {
        PageHelper.startPage(farmerLoanSheetDTO.getCurrentPage(), farmerLoanSheetDTO.getPageSize());
        List<FarmerLoanSheetVO> farmerLoanSheetVOS = farmerLoanSheetMapper.selectFlsVOByFlsDTO(farmerLoanSheetDTO);
        return farmerLoanSheetVOS;

    }

    /**
     * 模糊查询待审查
     * @param farmerLoanSheetDTO
     * @return
     */
    @Override
    public List<FarmerLoanSheetVO> selectDscByFlsDTO(FarmerLoanSheetDTO farmerLoanSheetDTO) {
        PageHelper.startPage(farmerLoanSheetDTO.getCurrentPage(), farmerLoanSheetDTO.getPageSize());
        List<FarmerLoanSheetVO> farmerLoanSheetVOS = auditFormMapper.selectDscByFlsDTO(farmerLoanSheetDTO);
        return farmerLoanSheetVOS;
    }

    /**
     * lgy
     * 模糊查询待审批
     * @param farmerLoanSheetDTO
     * @return
     */
    @Override
    public List<FarmerLoanSheetVO> selectDspByFlsDTO(FarmerLoanSheetDTO farmerLoanSheetDTO) {
        PageHelper.startPage(farmerLoanSheetDTO.getCurrentPage(), farmerLoanSheetDTO.getPageSize());
        List<FarmerLoanSheetVO> farmerLoanSheetVOS = auditFormMapper.selectDspByFlsDTO(farmerLoanSheetDTO);
        return farmerLoanSheetVOS;
    }

    /**
     * lgy
     * 根据贷款详情id查详情
     * @param loanDetailsId
     * @return
     */
    @Override
    public FarmerLoanSheetVO selectXqByPrimaryKey(Long loanDetailsId) {
        FarmerLoanSheetVO farmerLoanSheetVO = farmerLoanSheetMapper.selectXqByPrimaryKey(loanDetailsId);
        FarmerLoanSheetDTO farmerLoanSheetDTO = new FarmerLoanSheetDTO();
        farmerLoanSheetDTO.setClinetDocumentsNumber(farmerLoanSheetVO.getClinetDocumentsNumber());
        List<FarmerLoanSheetVO> farmerLoanSheetVOS = farmerLoanSheetMapper.selectFls2VOByFlsDTO(farmerLoanSheetDTO);
        double b = 0;
        for (FarmerLoanSheetVO loanSheetVO : farmerLoanSheetVOS) {
            b+= Double.parseDouble(loanSheetVO.getLoanBy1());
        }
        farmerLoanSheetVO.setYdje(b);
        return farmerLoanSheetVO;
    }

    /**
     * lgy
     * 根据身份证查找客户信息
     * @param clinetDocumentsNumber
     * @return
     */
    @Override
    public FarmerLoanSheetVO selectByCdn(String clinetDocumentsNumber) {
        ClientInfoSheet clientInfoSheet = clientInfoSheetMapper.selectCisByCdn(clinetDocumentsNumber);
        if(clientInfoSheet!=null){
            List<FarmerLoanSheetVO> list = farmerLoanSheetMapper.selectByCdn(clinetDocumentsNumber);
            FarmerLoanSheetVO farmerLoanSheetVO = list.get(0);
            farmerLoanSheetVO.setLoanDetailsDate(new Date());
            Random random = new Random();
            int i = random.nextInt(10000);
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String s = sdf.format(date);
            s="LL"+s+i;
            farmerLoanSheetVO.setLoanDetailsContractNum(s);
            FarmerLoanSheetDTO farmerLoanSheetDTO = new FarmerLoanSheetDTO();
            farmerLoanSheetDTO.setClinetDocumentsNumber(clinetDocumentsNumber);
            List<FarmerLoanSheetVO> farmerLoanSheetVOS1 = farmerLoanSheetMapper.selectFls2VOByFlsDTO(farmerLoanSheetDTO);
            double b = 0;
            for (FarmerLoanSheetVO loanSheetVO : farmerLoanSheetVOS1) {
                b+= Double.parseDouble(loanSheetVO.getLoanBy1());
            }
            farmerLoanSheetVO.setYdje(b);
            return farmerLoanSheetVO;
        }else {
            FarmerLoanSheetVO farmerLoanSheetVO = null;
            return farmerLoanSheetVO;
        }

    }

    /**
     * lgy
     * 添加贷款详情信息
     * @param farmerLoanSheetVO
     * @return
     */
    @Override
    public int insertDk(FarmerLoanSheetVO farmerLoanSheetVO) {
        AuditForm auditForm = new AuditForm();
        FarmerLoanSheet farmerLoanSheet = new FarmerLoanSheet();
        List<Integer> detailsIds = farmerLoanSheetMapper.selectPrimaryKey();
        int i2 = detailsIds.get(0)+ 1;
        farmerLoanSheet.setLoanDetailsId(i2);
        auditForm.setApprovalStatusId(auditFormMapper.selectPrimaryKey().get(0)+1);
        auditForm.setApprovalStatus("待审查");
        int clientId = clientInfoSheetMapper.selectPrimaryKeyByCdn(farmerLoanSheetVO.getClinetDocumentsNumber());
        auditForm.setClientId(clientId);
        auditForm.setLoanDetailsId(farmerLoanSheet.getLoanDetailsId());
        int i = auditFormMapper.insertSelective(auditForm);
        farmerLoanSheet.setLoanDetailsNum(farmerLoanSheetVO.getLoanDetailsNum());
        farmerLoanSheet.setClientId(clientId);
        farmerLoanSheet.setLoanDetailsDate(farmerLoanSheetVO.getLoanDetailsDate());
        farmerLoanSheet.setLoanDetailsStartDate(farmerLoanSheetVO.getLoanDetailsStartDate());
        farmerLoanSheet.setLoanDetailsEndDate(farmerLoanSheetVO.getLoanDetailsEndDate());
        farmerLoanSheet.setLoanDetailsManager(farmerLoanSheetVO.getLoanDetailsManager());
        farmerLoanSheet.setLoanDetailsNetwork(farmerLoanSheetVO.getLoanDetailsNetwork());
        farmerLoanSheet.setLoanDetailsContractYn(farmerLoanSheetVO.getLoanDetailsContractYn());
        farmerLoanSheet.setLoanDetailsContractNum(farmerLoanSheetVO.getLoanDetailsContractNum());
        farmerLoanSheet.setApprovalStatusId(auditForm.getApprovalStatusId());
        farmerLoanSheet.setLoanBy1(farmerLoanSheetVO.getLoanBy1());
        int i1 = farmerLoanSheetMapper.insertSelective(farmerLoanSheet);
        return i+i1;
    }
}
